package com.example.demo.services.system;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;

public class ResultService {
    //用于处理返回操作的类
    public static final String NORMAL = "0";
    public static final String CLIENT_ERROR = "400";
    public static final String NOT_FOUND = "404";
    public static final String NOT_AUTH = "401";
    public static final String UNAUTH_ACCESS = "403";
    public static final String SERVER_ERROR = "500";

    /**
     * 返回默认成功返回值
     *
     * @return
     */
    public static Result getNormalResult() {
        return getResult(NORMAL, "ok", null);
    }

    /**
     * 返回未找到返回值
     *
     * @return
     */
    public static Result getNotFoundResult() {
        return getResult(NOT_FOUND, "not found", null);
    }

    /**
     * 返回未授权返回值
     *
     * @return
     */
    public static Result getNotAuthResult() {
        return getResult(NOT_AUTH, "not auth", null);
    }

    public static Result getUnauthAccessResult() {
        return getResult(UNAUTH_ACCESS, "no permission access", null);
    }

    /**
     * 返回服务器错误返回值
     *
     * @return
     */
    public static Result getServerErrorResult() {
        return getResult(SERVER_ERROR, "server error", null);
    }

    /**
     * 返回自定义返回值
     *
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static Result getResult(String code, String msg, Object data) {
        Result result = new Result();
        result.code = code;
        result.msg = msg;
        result.data = data;
        return result;
    }

    public static class Result {
        public String code;
        public String msg;
        public Object data;
    }
    public static boolean middlewareGetUnauthAccessResultJSON(HttpServletResponse response) throws IOException {
        middlewareGetResultJSON(response, ResultService.getNotAuthResult());
        return false;
    }

    public static boolean middlewareGetNotAuthResultJSON(HttpServletResponse response) throws IOException {
        middlewareGetResultJSON(response, ResultService.getNotAuthResult());
        return false;
    }

    public static boolean middlewareGetResultJSON(HttpServletResponse response, Result result) throws IOException {
        response.setContentType("application/json; charset=utf-8");
        PrintWriter writer = response.getWriter();
        if (!Objects.isNull(Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getAttribute("msg", RequestAttributes.SCOPE_REQUEST))) {
            //贴入返回值
            result.msg = (String) Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getAttribute("msg", RequestAttributes.SCOPE_REQUEST);
        }
        ObjectMapper objectMapper = new ObjectMapper();
        String str = objectMapper.writeValueAsString(result);
        writer.print(str);
        writer.close();
        response.flushBuffer();
        return false;
    }
}
